package com.ctrip.business;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.go.commons.utils.HttpClientUtil;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.entity.ContentType;

import java.util.HashMap;
import java.util.Map;

public class UserService {
    private static final String HOST = "localhost";
    private static final int PORT = 8082;
    public static Header[] headers;
    public static String cookie="_fpacid=09031124110231317451; GUID=09031124110231317451; _abtest_userid=afd7d7af-bd30-4d27-96b4-a73722fe05d2; Session=SmartLinkCode=U155952&SmartLinkKeyWord=&SmartLinkQuary=&SmartLinkHost=&SmartLinkLanguage=zh; traceExt=campaign=CHNbaidu81&adid=index; Mkt_UnionRecord=%5B%7B%22aid%22%3A%224897%22%2C%22timestamp%22%3A1511689488406%7D%5D; __zpspc=9.1.1511689455.1511689488.2%231%7Cbaidu%7Ccpc%7Cbaidu81%7C%25E6%2590%25BA%25E7%25A8%258B%25E6%2597%2585%25E8%25A1%258C%25E7%25BD%2591%7C%23; TOUR.VBKH5.SessionId=scfbil1ixtb1d2sgexxifzhz; ASP.NET_SessionSvc=MTAuOC4xODkuNTd8OTA5MHxqaW5xaWFvfGRlZmF1bHR8MTUxMTc0NzU4MjM5NA; ASP.NET_SessionId=yl25ysk2sydh4xaqcqsx4ixg; Package.Vendor.H5Interface.Token=bMfrt8Cs51I1esNtUzPdwoCQ5FO9Rijq; cticket=6956819AAB12B1C9A0412BD9FEA4D9233E31292D0C64DD79108C467BE9D37AE8; DUID=u=6C98536FDEC0EC1CC37D33914EEE709E&v=0; IsNonUser=F; UUID=AB0A2ACAFA5D4810BD0D22233BB389B0; IsPersonalizedLogin=F; UserLanguage=zh-cn; page_time=1512717761930%2C1512734831287%2C1512736652285%2C1512961633784%2C1512971164138%2C1513145713590%2C1513145716186%2C1513145720676%2C1513146220477%2C1513236047410%2C1513262612142%2C1513262633471%2C1513262634051%2C1513262639930%2C1513262655120%2C1513262663004%2C1513262664274%2C1513262701195%2C1513262705837%2C1513262709118%2C1513262722748%2C1513262723278%2C1513262725974%2C1513262728981%2C1513336397423; _RF1=122.224.250.35; _RSG=0qKh1JCyKxF_wRck1CUEa9; _RDG=28919445113568272509729877777e7098; _RGUID=fed4f0c4-c1ad-4d00-990e-78c06e99bebf; userInfo=%7B%22Id%22%3A%22c0f46bf5-74e5-4b57-8747-7b85fcb67573%22%2C%22Name%22%3A%22UID_M462874668%22%2C%22ProviderId%22%3A36823%2C%22ProviderName%22%3A%22%u6606%u660E%u60A6%u6E38%u65C5%u884C%u793E%u6709%u9650%u516C%u53F8%22%2C%22ProviderShortName%22%3A%22%u6606%u660E%u60A6%u6E38%u65C5%u884C%u793E%u6709%u9650%u516C%u53F8%22%2C%22UserGroupId%22%3A1000027014%2C%22UserGroupName%22%3A%22%u603B%u90E8%u7EC4%22%2C%22IsCompany%22%3A%22T%22%2C%22CtripUid%22%3A%22M2325172410%22%2C%22AdvisorId%22%3A0%2C%22ExtNumber%22%3A%22%22%2C%22InChina%22%3Afalse%2C%22IdentityType%22%3A1%7D; _jzqco=%7C%7C%7C%7C1513336397634%7C1.1052293122.1511331012325.1513262728834.1513336397540.1513262728834.1513336397540.0.0.0.33.33; _ga=GA1.2.967315753.1511331012; _gid=GA1.2.2124374269.1513236048; _gat=1; _bfa=1.1511351078157.248hjw.1.1512648500843.1513336613986.41.503.10320641803";
    public static String userName;

    /**
     * 登录
     *
     * @param userName
     * @return
     * @throws Exception
     */
    public static Map<String, Object> check(String userName) throws Exception {
        Map<String, String> data = new HashMap<>();
        data.put("user_name", userName);
        String response = HttpClientUtil.postNotForm(String.format("http://%s:%d/vbooking/user/find", HOST
                , PORT), JSON.toJSONString(data), ContentType.APPLICATION_JSON);
        JSONObject json = JSON.parseObject(response);
        if ("0".equals(json.getString("code"))) {
            return json.getJSONObject("data");
        } else {
            throw new Exception(json.getString("message"));
        }
    }

    public static boolean login(String userName, String password) throws Exception {
        Map<String, String> params = new HashMap<>();
        params.put("txtOperid", userName);
        params.put("txtPwd", password);
        params.put("page_id", "107029");
        params.put("hiddLevel", "hiddLevel");
        params.put("__EVENTTARGET", "btnLogin");
        params.put("__EVENTARGUMENT", "");
        params.put("__VIEWSTATEGENERATOR", "FAA5D0E4");
        params.put("__EVENTVALIDATION", "/wEdAAnav2lTg5jcKHrn5kJj9CeoQZXMCK8oagkPl3DGCZ9KFTPSlu16Yx4QbiDU+dddK1ORPzXSpaWOnuCdtG7vWNQFnj8xYDGtYUcmfg+DuwkwNqKeKEbp39eHc9mbdvkCgxCQeIOVNAlC86mTASm7fybgyCTw37jAyMoZGwJcnyT2fniKWq/NxpE+I0+ui/PbipNVw/ENd4yX7ZV2BRDhYMflVYwAlQ==");
        params.put("__VIEWSTATE", "/wEPDwUKMTI2ODM4MTAwMw9kFgICAQ9kFgZmDxYCHgRocmVmBZ4BaHR0cHM6Ly9hY2NvdW50cy5jdHJpcC5jb20vbWVtYmVyL2xvZ2luLmFzcHg/YmFja3VybD1odHRwOi8vdmJvb2tpbmcuY3RyaXAuY29tL1BhY2thZ2VfVmVuZG9yX1Zib29raW5nL1VzZXJNYW5hZ2VtZW50L0xvZ2luMi5hc3B4P3ZpYT1jdHJpcCZyZXNwb25zZW1ldGhvZD1nZXRkAgUPFgIeBXN0eWxlBQ1kaXNwbGF5Om5vbmU7ZAILDxYCHwAFngFodHRwczovL2FjY291bnRzLmN0cmlwLmNvbS9tZW1iZXIvbG9naW4uYXNweD9iYWNrdXJsPWh0dHA6Ly92Ym9va2luZy5jdHJpcC5jb20vUGFja2FnZV9WZW5kb3JfVmJvb2tpbmcvVXNlck1hbmFnZW1lbnQvTG9naW4yLmFzcHg/dmlhPWN0cmlwJnJlc3BvbnNlbWV0aG9kPWdldGQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgEFCUltZ0J0blZmY0zg3BnsPwv97juwrtSEWHzF9tfJ");

        HttpResponse response = HttpClientUtil.postFormAndResultHttpResponse("https://vbooking.ctrip.com/Package_Vendor_Vbooking/UserManagement/Login2.aspx?redirect=http%3a%2f%2fvbooking.ctrip.com%2fPackage_Vendor_Vbooking%2fVBooking_Home%2fProvider_Home.aspx", params);
        headers = response.getAllHeaders();
        return true;
    }


    public static void initData(String cookie){
        UserService.cookie = cookie;
        String[] cookies = cookie.split("; ");
        for (String s : cookies) {
            if (s.startsWith("loginCookie=")) {
                String[] loginCookies = s.split("loginCookie=")[1].split("&");
                for (String loginCookie : loginCookies) {
                    if (loginCookie.startsWith("Name=")) {
                        userName = loginCookie.split("Name=")[1];
                    }
                }
            }
        }
    }

}
